require('dotenv').config();

// 导入各模块配置
const dbConfig = require('./db');
const authConfig = require('./auth');

// 应用基础配置
const appConfig = {
  port: process.env.PORT || 3000,
  environment: process.env.NODE_ENV || 'development',
  apiPrefix: process.env.API_PREFIX || '/api/v1',
  logLevel: process.env.LOG_LEVEL || 'info',
  allowedOrigins: (process.env.ALLOWED_ORIGINS || 'http://localhost:3000').split(','),
  
  // 文件上传配置
  fileUpload: {
    maxSize: 10 * 1024 * 1024, // 10MB
    allowedFormats: ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx'],
    uploadPath: './public/uploads'
  },
  
  // 响应配置
  response: {
    successCode: 200,
    errorCode: 500,
    notFoundCode: 404,
    unauthorizedCode: 401,
    forbiddenCode: 403,
    validationErrorCode: 400
  },
  
  // 分页配置
  pagination: {
    defaultPage: 1,
    defaultLimit: 10,
    maxLimit: 100
  }
};

// 日志配置
const logConfig = {
  level: appConfig.logLevel,
  format: appConfig.environment === 'development' ? 'dev' : 'combined',
  
  // 文件日志配置
  file: {
    enabled: appConfig.environment !== 'development',
    path: './logs',
    filename: 'server.log',
    maxsize: 5242880, // 5MB
    maxFiles: 5
  }
};

// 导出统一的配置对象
const config = {
  app: appConfig,
  db: dbConfig,
  auth: authConfig,
  log: logConfig
};

// 将配置设置为全局变量，方便在其他模块中使用
global.config = config;

module.exports = config;

// 导出配置加载状态
module.exports.isConfigLoaded = true;